package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.workshop.CreateWorkshopRequest;
import org.dxy.api.dto.request.workshop.UpdateWorkshopRequest;
import org.dxy.api.dto.response.R;
import org.dxy.infrastructure.persistent.po.query.WorkshopViewPo;
import org.dxy.trigger.http.service.WorkshopApplicationService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车间管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/workshops")
@RequiredArgsConstructor
public class WorkshopController {
    
    private final WorkshopApplicationService workshopApplicationService;
    
    /**
     * 创建车间
     */
    @PostMapping("/create")
    public R<Long> createWorkshop(@Validated @RequestBody CreateWorkshopRequest request) {
        return workshopApplicationService.create(request);
    }
    
    /**
     * 分页查询车间列表
     */
    @GetMapping("/page")
    public R<Page<WorkshopViewPo>> getWorkshopList(
            @ParameterObject Page<WorkshopViewPo> page,
            @ParameterObject WorkshopViewPo query) {
        return workshopApplicationService.list(page, query);
    }
    
    /**
     * 更新车间
     */
    @PutMapping("/{id}")
    public R<Void> updateWorkshop(@PathVariable Long id, @Validated @RequestBody UpdateWorkshopRequest request) {
        return workshopApplicationService.update(id, request);
    }
    
    /**
     * 删除车间
     */
    @DeleteMapping("/{id}")
    public R<Void> deleteWorkshop(@PathVariable Long id) {
        return workshopApplicationService.delete(id);
    }
    
    /**
     * 获取车间详情
     */
    @GetMapping("/{id}")
    public R<WorkshopViewPo> getWorkshop(@PathVariable Long id) {
        return workshopApplicationService.getById(id);
    }
    
    /**
     * 根据工厂ID查询车间列表
     */
    @GetMapping("/factory/{factoryId}")
    public R<List<WorkshopViewPo>> getWorkshopsByFactory(@PathVariable Long factoryId) {
        return workshopApplicationService.getByFactoryId(factoryId);
    }
    
    /**
     * 启用车间
     */
    @PutMapping("/{id}/enable")
    public R<Void> enableWorkshop(@PathVariable Long id) {
        return workshopApplicationService.enable(id);
    }
    
    /**
     * 禁用车间
     */
    @PutMapping("/{id}/disable")
    public R<Void> disableWorkshop(@PathVariable Long id) {
        return workshopApplicationService.disable(id);
    }
    
    /**
     * 设置车间为维护状态
     */
    @PutMapping("/{id}/maintenance")
    public R<Void> setWorkshopMaintenance(@PathVariable Long id) {
        return workshopApplicationService.setMaintenance(id);
    }
    
    /**
     * 根据工厂ID获取启用状态的车间列表
     */
    @GetMapping("/factory/{factoryId}/enabled")
    public R<List<WorkshopViewPo>> getEnabledWorkshopsByFactory(@PathVariable Long factoryId) {
        return workshopApplicationService.getEnabledByFactoryId(factoryId);
    }
}